package red.mlz.app.controller.index;

import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import red.mlz.app.domain.material.*;
import red.mlz.module.module.category.entity.Category;
import red.mlz.module.module.category.service.CategoryService;
import red.mlz.module.module.material.entity.Material;
import red.mlz.module.module.material.service.MaterialService;
import red.mlz.module.utils.Response;
import red.mlz.module.utils.SpringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;


@RestController
@Slf4j
public class IndexController {


    @Autowired
    private MaterialService materialService;

    @Autowired
    private CategoryService categoryService;

    private final ExecutorService executor = new ThreadPoolExecutor(
            4, 4,
            0L, TimeUnit.MILLISECONDS,
            new ArrayBlockingQueue<>(1000),  // 有界队列（容量1000）
            new ThreadFactory() {
                @Override
                public Thread newThread(Runnable r) {
                    Thread thread = new Thread(r);
                    thread.setName("sms-" + thread.getId());
                    return thread;
                }
            },
            new ThreadPoolExecutor.AbortPolicy()  // 队列满时抛出RejectedExecutionException
    );

    /**
     * 获取首页列表
     *
     * @return
     */
    @RequestMapping("/index")
    public Response getIndex() {
        FrontVo frontVo = new FrontVo();
        CountDownLatch countDownLatch = new CountDownLatch(5);

        executor.submit(() -> {
            // 1.banner,image,link
            List<BannerVo> bannerVoList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                BannerVo bannerVo = new BannerVo();
                bannerVo.setBanner("轮播图" + i);
                bannerVo.setImage("imagexxx" + i + ".png");
                bannerVo.setLink("http://ssss" + i + ".com");
                bannerVoList.add(bannerVo);
            }
            frontVo.setBannerVoList(bannerVoList);
            countDownLatch.countDown();
        });
        executor.submit(() -> {
            // 2.分类(一级分类取出来)
            List<Category> categoryList = categoryService.listByParentId("0");
            frontVo.setCategoryList(categoryList);
            countDownLatch.countDown();
        });
        executor.submit(() -> {
            // 3.频道，image，title,频道id
            List<ChannelVo> channelVoList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                ChannelVo channelVo = new ChannelVo();
                channelVo.setChannelId(i);
                channelVo.setImage("imagexxx" + i + ".png");
                channelVo.setTitle("频道" + i);
                channelVoList.add(channelVo);
            }
            frontVo.setChannelVoList(channelVoList);
            countDownLatch.countDown();
        });
        executor.submit(() -> {
            // 4.获得，image，title,活动id
            List<ActivityVo> activityVoList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                ActivityVo activityVo = new ActivityVo();
                activityVo.setActivityId(i);
                activityVo.setImage("imagexxx" + i + ".png");
                activityVo.setTitle("频道" + i);
                activityVoList.add(activityVo);
            }
            frontVo.setActivityVoList(activityVoList);
            countDownLatch.countDown();
        });
        executor.submit(() -> {
            // 5.推荐内容
            MaterialWpVo baseWp = new MaterialWpVo();
            baseWp.setPage(1);
            String pageSize = SpringUtils.getProperty("application.pagesize");
            List<Material> materialList = materialService.getMaterialList(baseWp.getPage(), Integer.parseInt(pageSize), null, null);
            frontVo.setMaterialList(materialList);
            countDownLatch.countDown();
        });
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return new Response(1001, frontVo);
    }
}
